package com.yiwei.controller.utils;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentNavigableMap;

import org.mapdb.DB;
import org.mapdb.DBMaker;

public class MapDbUtils {
	public static void main(String[] args) {
//		DB db = DBMaker.newFileDB(new File(Constant.dbName)).closeOnJvmShutdown().encryptionEnable("123").make();
//
//		ConcurrentNavigableMap map = db.getTreeMap(Constant.tableName);
//
//		map.put(1, "one");
//		map.put(2, "two");
//
//		db.commit();
//
//		map = db.getTreeMap(Constant.tableName);
//		System.out.println(map);
//
//		db.close();
		
		MapDbUtils.put(Constant.dbName, Constant.tableName, "bb", "22222");
		
		Map mapDb = MapDbUtils.get(Constant.dbName, Constant.tableName);
		System.out.println(mapDb);
	}
	
	public static void put(String dbName,String tableName,String key,String value) {
		DB db = DBMaker.newFileDB(new File(dbName)).closeOnJvmShutdown().encryptionEnable("123").make();
		ConcurrentNavigableMap map = db.getTreeMap(tableName);
		map.put(key, value);
		db.commit();
		db.close();
	}
	
	public static Map get(String dbName,String tableName) {
		DB db = DBMaker.newFileDB(new File(dbName)).closeOnJvmShutdown().encryptionEnable("123").make();
		ConcurrentNavigableMap map = db.getTreeMap(tableName);
		Map mapRet = new HashMap();
		Set<String> key = map.keySet();
		for (Iterator it = key.iterator(); it.hasNext();) {
			String s = (String) it.next();
			mapRet.put(s,map.get(s));
		}
		db.close();
		
		return mapRet;
	}
}
